package dingding;

import com.alibaba.fastjson.JSONObject;
import com.dingtalk.api.DefaultDingTalkClient;
import com.dingtalk.api.DingTalkClient;
import com.dingtalk.api.request.OapiAttendanceScheduleListbyusersRequest;
import com.dingtalk.api.request.OapiAttendanceScheduleShiftListbydaysRequest;
import com.dingtalk.api.request.OapiGettokenRequest;
import com.dingtalk.api.response.OapiAttendanceScheduleListbyusersResponse;
import com.dingtalk.api.response.OapiAttendanceScheduleShiftListbydaysResponse;
import com.dingtalk.api.response.OapiGettokenResponse;
import com.ruoyi.common.core.utils.bean.BeanUtils;
import com.ruoyi.teacher.domain.TTeacherRostering;
import com.ruoyi.teacher.service.ITTeacherRosteringService;
import com.taobao.api.ApiException;
import org.springframework.beans.factory.annotation.Autowired;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

public class  Demo{

    @Autowired
    private static ITTeacherRosteringService itTeacherRosteringService;

    // 应用的appKey
    public static String appKey = "dingod4uaw4goav3zmav";
    // 应用的appSecret
    public static String appSecret = "5Eax_ynl070y5dpwqfAgJ0Ulnsw-GwGOazhOWE2t47UqZXTcBO1ek-oWy2mE4tu-";

    public static String opUserId = "manager2215";
    
    /**
     * 通过调用接口获取access_token
     * dingtalk
     * @return
     */
    public static String getAccessToken() {
        DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/gettoken");
        OapiGettokenRequest request = new OapiGettokenRequest();
        request.setAppkey(appKey); 
        request.setAppsecret(appSecret);
        request.setHttpMethod("GET");
        OapiGettokenResponse response = null;
        try {
            response = client.execute(request);
        } catch (ApiException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return response.getAccessToken();
    }
   
    public static void main(String[] args) throws ApiException {
//        Long timeStamp = System.currentTimeMillis();
//        // 获取access_token
//        String access_token = getAccessToken();
//        // 批量查询人员排班信息(时间段)
//        String rosterInfo = getRosterInfo(timeStamp, access_token);
//        String result = JSONObject.parseObject(rosterInfo).getString("result");
//        List<TTeacherRostering> tTeacherRosterings = JSONObject.parseArray(result, TTeacherRostering.class);
//        for (TTeacherRostering tTeacherRostering : tTeacherRosterings) {
//            System.out.println(tTeacherRostering);
//            itTeacherRosteringService.insertTTeacherRostering(tTeacherRostering);
//        }
//        getBatchRosterSummary(timeStamp, access_token);

        String time = "2022-12-26";
        //设置时间模版
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date date = null;
        try {
            date = simpleDateFormat.parse(time);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        Long workDate = date.getTime();
        System.out.println(workDate);
    }

    /**
     * 批量查询成员排班概要信息
     * 调用本接口，查询用户在某个时间段内的排班概要信息。
     * 调用本接口，获取成员排班概要信息，包括班次名称、班次版本ID、考勤组ID等内容
     * @param access_token
     * @throws ApiException
     */
    private static void getBatchRosterSummary(Long timeStamp, String access_token) throws ApiException {
        DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/attendance/schedule/shift/listbydays");
        OapiAttendanceScheduleShiftListbydaysRequest req = new OapiAttendanceScheduleShiftListbydaysRequest();
        req.setOpUserId(opUserId);
        req.setUserids("manager123,015331436801841164");
        req.setFromDateTime(1671947413000L);
        req.setToDateTime(timeStamp);
        OapiAttendanceScheduleShiftListbydaysResponse rsp = client.execute(req, access_token);
        System.out.println(rsp.getBody());
    }

    /**
     * 批量查询人员排班信息
     * 调用本接口，批量查询员工在某天的排班信息。
     * 例如，员工小钉和小明所在的考勤组信息如下图所示，考勤组内设置的班次为默认班次，上班时间为09：30，下班时间为18：30。
     * 调用本接口，可获取小钉和小明2022年6月14日的排班信息，小钉和小明在2022年6月14日的班次信息和上下班时间等。
     * @param timeStamp
     * @param access_token
     * @throws ApiException
     */
    private static String getRosterInfo(Long timeStamp, String access_token) throws ApiException {
        DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/attendance/schedule/listbyusers");
        OapiAttendanceScheduleListbyusersRequest req = new OapiAttendanceScheduleListbyusersRequest();
        req.setOpUserId(opUserId);
        req.setUserids("manager2215,015331436801841164");
        req.setFromDateTime(timeStamp);
        req.setToDateTime(1672206613000L);
        OapiAttendanceScheduleListbyusersResponse rsp = client.execute(req, access_token);
        return rsp.getBody();
    }
}
